package com.hrzyit.whistle.comm.ui.fragment

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.hrzyit.base.common.BaseConstant
import com.hrzyit.base.ui.fragment.BaseMvpFragment
import com.hrzyit.base.utils.AppPrefsUtils
import com.hrzyit.whistle.comm.R
import com.hrzyit.whistle.comm.common.CommConstant
import com.hrzyit.whistle.comm.data.bean.DepartmentEntity
import com.hrzyit.whistle.comm.injection.component.DaggerContactCenterComponent
import com.hrzyit.whistle.comm.injection.module.ContactCenterModule
import com.hrzyit.whistle.comm.presenter.DepartmentPresenter
import com.hrzyit.whistle.comm.presenter.view.DepartmentView
import com.hrzyit.whistle.comm.ui.activity.ContactMainActivity
import com.hrzyit.whistle.comm.ui.adapter.DepartmentAdapter
import kotlinx.android.synthetic.main.activity_department.*
import me.yokeyword.indexablerv.IndexableLayout
import org.jetbrains.anko.support.v4.startActivity

/**
 * 通讯录：单位 Fragment
 * @author YangWei
 * @date 2019/4/19
 */
class DepartmentFragment : BaseMvpFragment<DepartmentPresenter>(), DepartmentView {

    private lateinit var indexAdapter: DepartmentAdapter

    private lateinit var activity: Activity

    override fun onAttach(context: Context?) {
        super.onAttach(context)
        activity = context as Activity

    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        return inflater.inflate(R.layout.fragment_department, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initAdapter()

        initData()

    }


    /**
     * 初始化单位 IndexableLayout
     */
    private fun initAdapter() {
        // 初始化单位索引选择器
        // 适配器
        val indexLayout = departmentIndexLayout as IndexableLayout
        indexLayout.setLayoutManager(LinearLayoutManager(activity))

        // setAdapter
        indexAdapter = DepartmentAdapter(activity)
        indexLayout.setAdapter(indexAdapter)

        // set Material Design OverlayView
        indexLayout.setOverlayStyle_MaterialDesign(Color.BLUE)

        // 全字母排序。  排序规则设置为：每个字母都会进行比较排序；速度较慢
        indexLayout.setCompareMode(IndexableLayout.MODE_FAST)

        // 单位栏目点击事件
        indexAdapter.setOnItemContentClickListener { _, _, _, entity ->
            // 跳转到通讯录联系人界面 Fragment
            startActivity<ContactMainActivity>(
                "fragment_key" to CommConstant.RESULT_CODE_CONTACT_CONTACTS, "deptId" to entity.id
            )
        }
        // 单位标题栏目点击事件
//        indexAdapter.setOnItemTitleClickListener { _, currentPosition, indexTitle ->
//        }
    }

    /**
     * 加载数据
     */
    private fun initData() {
        val userId = AppPrefsUtils.getLong(BaseConstant.KEY_SP_USER_TOKEN)
        mPresenter.selectContactDeptList(userId)
    }


    /**
     * Dagger2 注入
     */
    override fun injectComponent() {
        DaggerContactCenterComponent.builder()
            .activityComponent(mActivityComponent)
            .contactCenterModule(ContactCenterModule())
            .build()
            .inject(this)
        mPresenter.mView = this
    }

    override fun onGetSelectContactDeptListSuccess(result: MutableList<DepartmentEntity>) {
        indexAdapter.setDatas(result)
    }
}